package gob.pgr.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.commons.lang.StringUtils;

/**
 * Servlet implementation class AjaxServlet
 */
@WebServlet("/AjaxServlet")
public class AjaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String[] lenguajes;

    /**
     * Default constructor. 
     */
    public AjaxServlet() {
        // TODO Auto-generated constructor stub
    }
    
    

	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
		this.lenguajes = new String[] {
			"Action Script",
			"Ada",
			"C++",
			"C#",
			"Eiffel",
			"Fortran",
			"Java",
			"Javascript",
			"Objective-C",
			"Perl",
			"PHP",
			"Power Builder",
			"Python",
			"Ruby",
			"Smalltalk",
			"VB.NET",
			"Visual FoxPro"
		};
	}



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesar(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		procesar(request, response);
	}

	private void procesar(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String metodo = StringUtils.trimToEmpty(request.getParameter("metodo"));
		String respuesta = "";
		if ("autocomplete".equals(metodo)) {
			String texto = StringUtils.trimToEmpty(request.getParameter("userid"));
			List<String> lenguajes = autocomplete(texto);
			String html = "";
			for (String string : lenguajes) {
				html += "<li>" + string + "</li>";
			}
			respuesta = html;
		}
		else if ("json".equals(metodo)) {
			List<EjemploJson> lista = new ArrayList<EjemploJson>();
			lista.add(new EjemploJson("Te", "1"));
			lista.add(new EjemploJson("Cafe", "2"));
			lista.add(new EjemploJson("Jugo", "3"));
			respuesta = JSONArray.fromObject(lista).toString();
		}
		response.setContentType("text/plain");
		response.setContentLength(respuesta.getBytes().length);
		response.getOutputStream().write(respuesta.getBytes());
	}
	
	private List<String> autocomplete(String texto) {
		List<String> lista = Collections.emptyList();
		lista = new ArrayList<String>();
		for (String lenguaje : this.lenguajes) {
			if (StringUtils.upperCase(lenguaje).startsWith(
					StringUtils.upperCase(texto)
				)) lista.add(lenguaje);
		}
		return lista;
	}
	
}